﻿namespace WXWorkSvc.Common.TagHelpers;
/// <summary>
/// 24.1.2修改完成 
/// </summary>
[HtmlTargetElement("wx-buttons", TagStructure = TagStructure.WithoutEndTag)]
public class WxButtonsTagHelper : TagHelper
{
    private readonly IHtmlGenerator _HtmlGenerator;
    [HtmlAttributeNotBound]
    [ViewContext]
    public ViewContext ViewContext { get; set; } = default!;
    public WxButtonsTagHelper(IHtmlGenerator generator)
    {
        _HtmlGenerator = generator;
    }
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-link-text-")]
    public Dictionary<string, string> PageLinkTexts { get; set; } = new();
    /// <summary>
    /// 若为空，则onclick(asp-route-values..)
    /// </summary>
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-page-")]
    public Dictionary<string, string> PageNames { get; set; } = new();
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-fun-")]
    public Dictionary<string, string> FunctionNames { get; set; } = new();
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-link-type-")]
    public Dictionary<string, EButtonType> LinkTypes { get; set; } = new();
    /// <summary>
    /// 常量参数
    /// </summary>
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-const-")]
    public Dictionary<string, object> ConstValues { get; set; } = new();
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-route-")]
    public Dictionary<string, ModelExpression> RouteValues { get; set; } = new();
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-delete-")]
    public Dictionary<string, bool> Deletes { get; set; } = new();

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        output.TagName = "div";
        output.TagMode = TagMode.StartTagAndEndTag;
        output.Attributes.Add("class", "weui-form-preview");

        var tagDiv = new TagBuilder("div");
        tagDiv.AddCssClass("weui-form-preview__ft");


        foreach (var kv in PageLinkTexts)
        {
            if (Deletes.TryGetValue(kv.Key, out var delete) && delete)
                continue;
            var routeValues = RouteValues.GetRouteValues(ConstValues, kv.Key);
            if (!LinkTypes.TryGetValue(kv.Key, out var linkType))
                linkType = EButtonType.Secondary;
            var cssClass = TagHelperExtend.GetButtonCssClass(linkType);

            if (PageNames.Remove(kv.Key, out var pageName))
            {
                var linkTagBuilder = _HtmlGenerator.GeneratePageLink(ViewContext, kv.Value, pageName, null, null, null, null, routeValues, new { @class = cssClass });
                tagDiv.InnerHtml.AppendHtml(linkTagBuilder);
            }
            else if (FunctionNames.Remove(kv.Key, out var functionName))
            {
                var linkTagBuilder = new TagBuilder("a");
                linkTagBuilder.AddCssClass(cssClass);
                linkTagBuilder.Attributes.Add("href", "javascript:;");
                linkTagBuilder.Attributes.Add("onclick", routeValues.ToFunction(functionName));
                linkTagBuilder.InnerHtml.SetHtmlContent(kv.Value);
                tagDiv.InnerHtml.AppendHtml(linkTagBuilder);
            }
            else
            {
                var buttonTagBuilder = new TagBuilder("button");
                buttonTagBuilder.AddCssClass(cssClass);
                buttonTagBuilder.Attributes.Add("type", "submit");
                buttonTagBuilder.InnerHtml.SetContent(kv.Value);
                tagDiv.InnerHtml.AppendHtml(buttonTagBuilder);
            }

        }

        output.Content.SetHtmlContent(tagDiv);



    }

}

